package EBM.GUI;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextArea;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import EBM.BLL.SocketConnection;
import EBM.util.SystemTime;

 
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Startup extends org.eclipse.swt.widgets.Composite implements ActionListener{
	private Label lblHost;
	private Text txtHost;
	private Button button2;
	private JTextArea jTextArea1;
	private Panel panel2;
	private Label lblPort;
	private JButton jButton1;
	private Frame frame2;
	private Composite composite2;
	private Panel panel1;
	private Frame frame1;
	private Composite composite1;
	private Button button1;
	private Label lblLog;
	private Button btnStop;
	private Text txtPort;

	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) {
		showGUI();
	}

	/**
	 * Auto-generated method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		Startup inst = new Startup(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		shell.setText("EBM服务器");
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
            	try{
        			SocketConnection.StopServe();
        		}catch(Exception ex){
        		}finally{
        			System.exit(0);
        		}
            }

});

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public Startup(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			this.setLayout(new FormLayout());
			this.setSize(521, 281);
			{
				button2 = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData button2LData = new FormData();
				button2LData.width = 50;
				button2LData.height = 23;
				button2LData.left =  new FormAttachment(0, 1000, 338);
				button2LData.top =  new FormAttachment(0, 1000, 127);
				button2.setLayoutData(button2LData);
				button2.setText("退出");
				button2.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						button2WidgetSelected(evt);
					}
				});
			}
			{
				button1 = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData button1LData = new FormData();
				button1LData.width = 70;
				button1LData.height = 23;
				button1LData.left =  new FormAttachment(0, 1000, 110);
				button1LData.top =  new FormAttachment(0, 1000, 127);
				button1.setLayoutData(button1LData);
				button1.setText("查看主机");
				button1.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						button1WidgetSelected(evt);
					}
				});
			}
			{
				lblLog = new Label(this, SWT.NONE);
				FormData lblLogLData = new FormData();
				lblLogLData.width = 33;
				lblLogLData.height = 15;
				lblLogLData.left =  new FormAttachment(0, 1000, 55);
				lblLogLData.top =  new FormAttachment(0, 1000, 156);
				lblLog.setLayoutData(lblLogLData);
				lblLog.setText("日志：");
			}
			{
				btnStop = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData btnStopLData = new FormData();
				btnStopLData.width = 56;
				btnStopLData.height = 21;
				btnStopLData.left =  new FormAttachment(0, 1000, 270);
				btnStopLData.top =  new FormAttachment(0, 1000, 127);
				btnStop.setLayoutData(btnStopLData);
				btnStop.setText("停止服务");
				btnStop.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						btnStopWidgetSelected(evt);
					}
				});
			}
			{
				FormData txtPortLData = new FormData();
				txtPortLData.width = 95;
				txtPortLData.height = 20;
				txtPortLData.left =  new FormAttachment(0, 1000, 108);
				txtPortLData.top =  new FormAttachment(0, 1000, 86);
				txtPort = new Text(this, SWT.BORDER|SWT.READ_ONLY);
				txtPort.setLayoutData(txtPortLData);
			}
			{
				lblPort = new Label(this, SWT.NONE);
				FormData lblPortLData = new FormData();
				lblPortLData.width = 48;
				lblPortLData.height = 13;
				lblPortLData.left =  new FormAttachment(0, 1000, 43);
				lblPortLData.top =  new FormAttachment(0, 1000, 93);
				lblPort.setLayoutData(lblPortLData);
				lblPort.setText("端口号：");
			}
			{
				FormData txtHostLData = new FormData();
				txtHostLData.width = 314;
				txtHostLData.height = 20;
				txtHostLData.left =  new FormAttachment(0, 1000, 108);
				txtHostLData.top =  new FormAttachment(0, 1000, 44);
				txtHost = new Text(this, SWT.BORDER|SWT.READ_ONLY);
				txtHost.setLayoutData(txtHostLData);
			}
			{
				lblHost = new Label(this, SWT.NONE);
				FormData lblHostLData = new FormData();
				lblHostLData.width = 36;
				lblHostLData.height = 13;
				lblHostLData.left =  new FormAttachment(0, 1000, 55);
				lblHostLData.top =  new FormAttachment(0, 1000, 51);
				lblHost.setLayoutData(lblHostLData);
				lblHost.setText("主机：");
			}
			{
				FormData composite1LData = new FormData();
				composite1LData.left =  new FormAttachment(0, 1000, 186);
				composite1LData.top =  new FormAttachment(0, 1000, 127);
				composite1LData.width = 76;
				composite1LData.height = 23;
				composite1 = new Composite(this, SWT.EMBEDDED);
				composite1.setLayoutData(composite1LData);
				{
					frame1 = SWT_AWT.new_Frame(composite1);
					{
						panel1 = new Panel();
						frame1.add(panel1);
						{
							jButton1 = new JButton();
							panel1.add(jButton1);
							jButton1.setText("启动服务");
							jButton1.setPreferredSize(new java.awt.Dimension(94, 21));
							jButton1.addActionListener(this);
						}
					}
				}
			}
			{
				FormData composite2LData = new FormData();
				composite2LData.left =  new FormAttachment(0, 1000, 100);
				composite2LData.top =  new FormAttachment(0, 1000, 156);
				composite2LData.width = 409;
				composite2LData.height = 108;
				composite2 = new Composite(this, SWT.EMBEDDED);
				composite2.setLayoutData(composite2LData);
				{
					frame2 = SWT_AWT.new_Frame(composite2);
					{
						panel2 = new Panel();
						frame2.add(panel2);
						{
							jTextArea1 = new JTextArea();
							jTextArea1.setLineWrap(true);
							panel2.add(jTextArea1);
							jTextArea1.setPreferredSize(new java.awt.Dimension(394, 113));
						}
					}
				}
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void btnStopWidgetSelected(SelectionEvent evt) {
		System.out.println("btnStop.widgetSelected, event="+evt);
		try{
			SocketConnection.StopServe();
			jTextArea1.setText(jTextArea1.getText()+SystemTime.GetCurrentTime()+" 关闭服务\n");
			jButton1.setEnabled(true);
		}catch(Exception ex){
			jTextArea1.setText(jTextArea1.getText()+SystemTime.GetCurrentTime()+" 关闭异常\n");
		}
	}

	private void button1WidgetSelected(SelectionEvent evt) {
		System.out.println("button1.widgetSelected, event="+evt);

		SocketConnection demo = new SocketConnection();
		txtHost.setText(demo.getIp().toString());
		txtPort.setText(String.valueOf(SocketConnection.PORT));
	}

	public void actionPerformed(ActionEvent e) {
		Object objcet = e.getSource(); 
		if(objcet == jButton1){ 			
			try{
				jButton1.setEnabled(false);
				jTextArea1.setText(jTextArea1.getText()+SystemTime.GetCurrentTime()+" 服务启动\n");
				SocketConnection.StartServe();
			}catch(Exception e1){
			}
		}
	}

	private void button2WidgetSelected(SelectionEvent evt) {
		System.out.println("button2.widgetSelected, event="+evt);
		try{
			SocketConnection.StopServe();
		}catch(Exception ex){
		}finally{
			System.exit(0);
		}
	}
}
